<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body  {
      margin: 0;
      padding: 0;
    }
    .box1, .box2 {
      position: absolute;
      width: 150px;
      height: 150px;
      line-height: 150px;
      text-align: center;
    }
    .box1 {
      background: blue;
      top: 30px;
    }
    .box2 {
      background: red;
      top: 190px;
    }
  </style>
</head>
<body>
  <button class="btn">按钮</button>
  <div class="box1">定时器</div>
  <div class="box2">请求动画</div>
  <script>
    const btn = document.querySelector('.btn')
    const box1 = document.querySelector('.box1')
    const box2 = document.querySelector('.box2')

    const setTimeoutFn = () => {
      let timer = null
      box1.style.left = '0px'
      timer = setInterval(() => {
        const leftVal = parseInt(box1.style.left)
        if (leftVal >= 720) {
          clearInterval(timer)
        } else {
          box1.style.left = leftVal + 1 + 'px'
        }
      }, 16)
    }
    const requestAnimationFrameFn = () => {
      box2.style.left = '0px'
      const callbackFn = () => {
        const leftVal = parseInt(box2.style.left)
        if (leftVal >= 720) {} else {
          box2.style.left = leftVal + 1 + 'px'
          window.requestAnimationFrame(callbackFn)
        }
      }
      window.requestAnimationFrame(callbackFn)
    }
    btn.addEventListener('click', () => {
      setTimeoutFn()
      requestAnimationFrameFn()
    })
  </script>
</body>
</html>